package com.raccoonagents.tools.cascet2d.util;

import com.raccoonagents.tools.cascet2d.model.Vector2f;


public class CalculationUtils {

	/**
	 * returns distance between 2 points
	 * @param o1
	 * @param o2
	 * @return
	 */
	public static double getDistance(final Vector2f location1, final Vector2f location2){
		final double xdiff = location1.x - location2.x;
		final double ydiff = location1.y - location2.y;				
		return Math.sqrt(xdiff * xdiff  + ydiff  * ydiff);				
	}
	/**
	 * returns distance without being squared by Math.sqrt
	 * @param location1
	 * @param location2
	 * @return
	 */
	public static double getDistanceSquared(final Vector2f location1, final Vector2f location2){
		final double xdiff = location1.x - location2.x;
		final double ydiff = location1.y - location2.y;				
		return (xdiff * xdiff  + ydiff  * ydiff);						
	}
	
	public static Vector2f getZRotatedVector(final float angle, final Vector2f vec){
		final float len = vec.getLength();
        final float x = (float) (Math.cos (Math.toRadians(angle)) * len);
        final float y = (float) (Math.sin (Math.toRadians(angle)) * len);
        return new Vector2f(x + vec.x,y + vec.y);
		
	}
	
	public static Vector2f getZUnitVector(final float angle){
        final float x = (float) (Math.cos (Math.toRadians(angle)));
        final float y = (float) (Math.sin (Math.toRadians(angle)));
        return new Vector2f(x,y);
		
	}
	
	public static Vector2f getVectorDiff(final Vector2f v1, final Vector2f v2){
		return new Vector2f(v1.x - v2.x, v1.y - v2.y);
	}
	
	public static Vector2f getMultVector(final Vector2f v2, final Vector2f v1){
		return new Vector2f(v2.x * v1.x, v1.y * v2.y);
	}

	public static float getDotProduct(final Vector2f v1, final Vector2f v2){
		return (v1.x * v2.x) + (v1.y * v2.y);
	}
	
	public static float getAngleBetween(final Vector2f v1, final Vector2f v2){
		final float dp = getDotProduct(v1, v2);
		final float lenMult = v1.getLength() * v2.getLength();
		final float result = (float) Math.toDegrees(Math.acos(dp / lenMult));
		return result;
	}
	

	public static Vector2f getAddVector(final Vector2f v1, final Vector2f v2){
		return new Vector2f(v1.x + v2.x, v1.y + v2.y);
	}
	
	/**
	 * return true is point is inside a rect with these values...
	 * @param position the left top corner of the rectangle
	 * @param size the size of rectangle
	 * @param point to check 
	 * @return
	 */
	public static boolean isPointInside(final Vector2f position, final Vector2f size, final Vector2f point){
		return (point.x >= position.x && point.x <= position.x + size.x && point.y >= position.y && point.y <= position.y + size.y);
	}
	
	/**
	/**
	 * Method to be used to find a point between two boxes where the second box is centered inside first one.
	 * @param size1 
	 * @param size2
	 * @return the position
	 */
	public static Vector2f getCenteredPosition(final Vector2f size1, final Vector2f size2){
		
		final float yDiff = ((size1.y - size2.y) / 2);
		final float xDiff = (size1.x - size2.x) / 2;
		return new Vector2f(xDiff, yDiff);
		
	}
}
